package com.igeek.config;

import ch.qos.logback.classic.spi.EventArgUtil;
import com.igeek.entity.Pet;
import com.igeek.interceptors.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * *User 訫
 * *Description TODO
 * *Date 2024/8/16
 **/

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebMvcConfigurer.super.addResourceHandlers(registry);
        //配置本地服务器的映射
        registry.addResourceHandler("/pic/**").addResourceLocations("file:/D:\\极客营133\\spring_boot\\1.SpringBoot\\pic\\");
    }

    //配置自定义类型转换器
    @Override
    public void addFormatters(FormatterRegistry registry) {
        WebMvcConfigurer.super.addFormatters(registry);
        registry.addConverter(new Converter<String,Pet>() {
            @Override
            public Pet convert(String s) {
                String[] strings = s.split("[&]");
                Pet pet = new Pet(strings[0],strings[1]);
                return pet;
            }
        });
    }

    //配置自定义过滤器

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**") //拦截所有请求,包括静态资源请求
                .excludePathPatterns("/login","/login.html","/css/**","/fonts/**","/images/**","/js/**"); //放行登录请求
    }
}
